//
//  YKUIConst.h
//  YKOCExection
//
//  Created by edward on 2021/8/18.
//

#ifndef YKUIConst_h
#define YKUIConst_h

/*** 常用 ***/
#define YKScreenW [UIScreen mainScreen].bounds.size.width
#define YKScreenH [UIScreen mainScreen].bounds.size.height
#define YKScreenBounds [UIScreen mainScreen].bounds

#define YKScale (IS_IPAD ? 1.5 : 1)

#define YK_Size(size) ((size) * (IS_IPAD ? 1.7 : YKScreenW / 375.0f))
#define YK_FontSize(size) ((size) * (IS_IPAD ? 1.7 : YKScreenW / 375.0f))
#define YK_SystemFont(size) [UIFont systemFontOfSize:YK_FontSize(size)]
#define YK_BoldFont(size) [UIFont boldSystemFontOfSize:YK_FontSize(size)]
#define YK_MediumFont(size) [UIFont systemFontOfSize:YK_FontSize(size) weight:UIFontWeightMedium]
#define YK_WeightFont(size,UIFontWeight) [UIFont systemFontOfSize:YK_FontSize(size) weight:UIFontWeight]

//判断是否为iPhone X系列
#define iPhoneX \
({BOOL isPhoneX = NO;\
if (@available(iOS 11.0, *)) {\
isPhoneX = [[UIApplication sharedApplication] delegate].window.safeAreaInsets.bottom > 0.0;\
}\
(isPhoneX);})

//X系列全面屏安全距离
#define kSafeAreaTopHeight (iPhoneX ? 44 : 20)
#define kSafeAreaBottomHeight (iPhoneX ? 34 : 0)

// TabBar高度
#define KTabBar_HEIGHT (iPhoneX ? 83 : 49)

// 底部高度
#define KBottom_HEIGHT (iPhoneX ? 34 : 0)

#define KNAV_HEIGHT self.navigationController.navigationBar.height
#define KSTATUSBAR_HEIGHT [UIApplication sharedApplication].statusBarFrame.size.height
#define KSTATUSBAR_HEIGHT_13 [UIApplication sharedApplication].keyWindow.windowScene.statusBarManager.statusBarFrame.size.height

// 顶部高度
#define KTOP_MARGIN (KSTATUSBAR_HEIGHT + 44)

// 顶部高度
#define KTOP_MARGIN (KSTATUSBAR_HEIGHT + 44)

// 是否高于IOS X
#define YK_isHigherIOS(version)    [[[UIDevice currentDevice]systemVersion]floatValue] > version

// 是否iPad
#define isPad                   (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)

// 手机型号
#define IS_IPAD (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
#define IS_IPHONE (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)
#define IS_RETINA ([[UIScreen mainScreen] scale] >= 2.0)

#define SCREEN_WIDTH ([[UIScreen mainScreen] bounds].size.width)
#define SCREEN_HEIGHT ([[UIScreen mainScreen] bounds].size.height)
#define SCREEN_MAX_LENGTH (MAX(SCREEN_WIDTH, SCREEN_HEIGHT))
#define SCREEN_MIN_LENGTH (MIN(SCREEN_WIDTH, SCREEN_HEIGHT))


NS_INLINE CGFloat lengthScale(CGFloat originalLength) {
    CGFloat scale = 375.0 / UIScreen.mainScreen.bounds.size.width;
    return (originalLength / scale > originalLength ? originalLength:originalLength / scale);
}


NS_INLINE UIColor * YKHEXCOLOR_(NSString *hexColor, CGFloat alpha) {
    //删除字符串中的空格
    NSString *cString = [[hexColor stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] uppercaseString];
    // String should be 6 or 8 characters
    if ([cString length] < 6)
    {
        return [UIColor clearColor];
    }
    // strip 0X if it appears
    //如果是0x开头的，那么截取字符串，字符串从索引为2的位置开始，一直到末尾
    if ([cString hasPrefix:@"0X"])
    {
        cString = [cString substringFromIndex:2];
    }
    //如果是#开头的，那么截取字符串，字符串从索引为1的位置开始，一直到末尾
    if ([cString hasPrefix:@"#"])
    {
        cString = [cString substringFromIndex:1];
    }
    if ([cString length] != 6)
    {
        return [UIColor clearColor];
    }
    
    // Separate into r, g, b substrings
    NSRange range;
    range.location = 0;
    range.length = 2;
    //r
    NSString *rString = [cString substringWithRange:range];
    //g
    range.location = 2;
    NSString *gString = [cString substringWithRange:range];
    //b
    range.location = 4;
    NSString *bString = [cString substringWithRange:range];
    
    // Scan values
    unsigned int r, g, b;
    [[NSScanner scannerWithString:rString] scanHexInt:&r];
    [[NSScanner scannerWithString:gString] scanHexInt:&g];
    [[NSScanner scannerWithString:bString] scanHexInt:&b];
    return [UIColor colorWithRed:((float)r / 255.0f) green:((float)g / 255.0f) blue:((float)b / 255.0f) alpha:alpha];
}

#define YKHEXCOLOR(hexColor) YKHEXCOLOR_(hexColor, 1.0f)

#endif /* YKUIConst_h */
